package com.assist.beans;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "indexBean")
@SessionScoped
public class IndexBean implements Serializable{



	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
    private String firstName;
    
    @ManagedProperty(value="#{loginBean}")
    LoginBean loginBean = null;

	public IndexBean() {
		// TODO Auto-generated constructor stub 
	System.out.println("Index bean");
	}
	
	public String getFirstName() {
		if (loginBean!=null) {
			firstName = loginBean.getUserName();			
		} 
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}
	
	public void logout(ActionEvent e) {
	System.out.println("Logout clicked!");

	try {
		//user=null;
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.invalidate();
		FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
		} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
    }
	
	
	public void redirectToUpdateUserInfo(ActionEvent e) {
		System.out.println("Redirect to show user info clicked!");

		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("showUserInfo.xhtml");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	public void redirectToCreateActivityBean(ActionEvent e) {
		System.out.println("Redirect to create activity clicked!");

		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("createActivity.xhtml");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	
}
 